export const name = "redirect"

export function apply(ctx) {
  function isUsage(session) {
    const s = session.stripped?.content
    if (!s) return false
    const prefix = session.resolve(ctx.root.config.prefix).find(p => s.startsWith(p))
    if (prefix != null) {
      const c = s.slice(prefix.length).split(/\s+/)[0]
      if (c.length > 1 && ctx.$commander.get(c)) return true
    }
    if (!prefix) {
      if (s.match(/^¿(¿|.*[.@])/s)) return true
      if (s.match(/^> |^∪|^< ?\|/s)) return true
      if (s.match(/#(ilo|填字|换算|查|randi)(\s|$)/)) return true
    }
    return false
  }

  const m = Object.create(null)
  ctx.middleware(session => {
    if (!isUsage(session)) return
    const now = Date.now()
    if (now - m[session.cid] < 300_000) return
    m[session.cid] = now
    return `<img src="file:///root/koishi-app/assets/newbotgroup.png"/>`
  }, true)
}
